<?php
namespace Content\Controller;
use Think\Controller;

/**
 * Class AdminTypeArticleController
 * @package Content\Controller
 * 文章控制器
 */
class AdminTypeArticleController extends Controller {

    private $model_name = 'Typearticle';

    public function index(){
        $data = filterInput($_GET);
        if(!isset($data['pid']) || !$data['pid']){
            return;
        }
        if(!checked_cat_priv(get_module_session('Admin', 'user_role'),$data['pid'] , 'show')){
            $this->error(LANG('no_cat_opr_acc'));
            return;
        }
        $this->assign('data', $data);
        $nav_model = D('Nav');
        $name = $nav_model->get_name($data['pid']);
        $this->assign('name', $name);
        $this->display();
    }

    public function get_all(){
        $data = filterInput($_GET);
        if(!isset($data['pid']) || !$data['pid']){
            return;
        }
        $model = D($this->model_name);
        $list = $model->queryField('type', $data['pid']);
        echo json_encode($list);
    }

    public function add(){
        $model = D($this->model_name);
        if(IS_POST){
            $data = filterInput($_POST);
            if(!checked_cat_priv(get_module_session('Admin', 'user_role'),$data['type'] , 'add')){
                $this->error(LANG('no_cat_opr_acc'));
                return;
            }
            $model->addObj($data, 'id');
            $this->success(LANG('save_success'));
            return;
        }else if(IS_GET){
            $data = filterInput($_GET);
            if(!isset($data['pid']) || !$data['pid']){
                return;
            }
            if(!checked_cat_priv(get_module_session('Admin', 'user_role'),$data['pid'] , 'add')){
                $this->error(LANG('no_cat_opr_acc'));
                return;
            }
            $this->assign('data', $data);
        }
        $this->display();
    }

    public function edit(){
        $model = D($this->model_name);
        if(IS_POST){
            $data = filterInput($_POST);
            if(!checked_cat_priv(get_module_session('Admin', 'user_role'),$data['nav'] , 'update')){
                $this->error(LANG('no_cat_opr_acc'));
                return;
            }
            $model->save($data);
            $this->success(LANG('save_success'));
            return;
        }else if(IS_GET){
            $data = filterInput($_GET);
            $obj = $model->queryObj($data['id'], 'id');
            $this->assign('obj', $obj);
            if(!checked_cat_priv(get_module_session('Admin', 'user_role'),$obj['type'] , 'update')){
                $this->error(LANG('no_cat_opr_acc'));
                return;
            }
        }
        $this->display();
    }

    public function deleteArray(){
        $data = filterInput($_POST);
        $ids = $data['ids'];
        $model = D($this->model_name);
        $one = $model->queryObj($ids[0], 'id');
        if(!checked_cat_priv(get_module_session('Admin', 'user_role'),$one['type'] , 'del')){
            $this->error(LANG('no_cat_opr_acc'));
            return;
        }

        $model->del($ids);
    }
}